//
//  CGServiceViewController.m
//  SchoolClient
//
//  Created by mac on 15/11/3.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGServiceViewController.h"
#import "CGFileReportViewController.h"
#import "CGFileMoveViewController.h"
#import "CGFileEvaluateViewController.h"
#import "CGApplyServiceViewController.h"

@interface CGServiceViewController ()
@property(nonatomic,strong)NSArray *titles;
@end

@implementation CGServiceViewController

-(NSArray *)titles{
    if (!_titles) {
        _titles = @[@"档案报道",@"档案转递",@"职称评定"];
    }
    return _titles;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    self.title = @"档案服务";
    
    [self initTableView];
    
    //添加一个按钮
    UIButton *submitButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 170, SCREEN_WIDTH - 40, 44)];
    [submitButton setTitle:@"申请档案服务" forState:UIControlStateNormal];
    [submitButton setBackgroundImage:[UIImage imageNamed:@"mine_btn_bg"] forState:UIControlStateNormal];
    [self.view addSubview:submitButton];
    [submitButton addTarget:self action:@selector(applyFileServer) forControlEvents:UIControlEventTouchUpInside];
    
    //使cell线条充满
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
}

-(void)initTableView{
    
    CGRect aRect = CGRectMake(0,
                              15,
                              SCREEN_WIDTH,
                              132);
    self.tableView = [[UITableView alloc] initWithFrame:aRect  style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.scrollEnabled = NO;
    [self.view addSubview: self.tableView];
    
    
    
    
}

#pragma mark 跳转到申请档案服务页面
-(void)applyFileServer{
    
    CGAccountDefault *user = [CGAccountDefault shareAccount];
    if (!user.login) {
        
        CGLoginViewController *loginVC = [CGLoginViewController new];
        [self.navigationController pushViewController:loginVC animated:YES];
        return;
    }
    
    if (!user.isVip) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"该服务仅供认证会员使用" delegate:self cancelButtonTitle:@"狠心放弃" otherButtonTitles:@"认证会员", nil];
        [alertView show];
        return;
    }
    
    CGApplyServiceViewController *applyServiceVC = [CGApplyServiceViewController new];
    [self.navigationController pushViewController:applyServiceVC animated:YES];
    
    
}



#pragma mark overide UITableViewDataSource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"tableviewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }
    cell.textLabel.text  = self.titles[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        CGFileReportViewController *reportVC = [CGFileReportViewController new];
        [self.navigationController pushViewController:reportVC animated:YES];
    }else if (indexPath.row == 1){
        CGFileMoveViewController *moveVC = [CGFileMoveViewController new];
        [self.navigationController pushViewController:moveVC animated:YES];
    }else{
        CGFileEvaluateViewController *evaluateVC = [CGFileEvaluateViewController new];
        [self.navigationController pushViewController:evaluateVC animated:YES];
    }
    
}



//设置表头表尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

#pragma mark alertView代理方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        //跳转支付宝界面
        CGPurchaseViewController *purchaseVC = [CGPurchaseViewController new];
        [self.navigationController pushViewController:purchaseVC animated:YES];
    }
}


//使cell的线条充满
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}


@end
